package com.agileai.pm.module.test.handler;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.annotation.PageAction;
import com.agileai.hotweb.controller.core.SimpleHandler;
import com.agileai.hotweb.renders.AjaxRenderer;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.cxmodule.ProjectDefectStatisticsManage;
import com.agileai.pm.domain.Consts;
import com.agileai.pm.domain.Project;
import com.agileai.util.StringUtil;
import com.agileai.util.XmlUtil;

/**
 * generated by miscdp
 */
public class ProjectDefectStatisticsManageHandler extends SimpleHandler{
	public ProjectDefectStatisticsManageHandler(){
		super();
	}
	
	public ViewRenderer prepareDisplay(DataParam param) {
		ProjectDefectStatisticsManage getService = lookupService(ProjectDefectStatisticsManage.class);
		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		String projectId = project.getProjectId();
		List<DataRow> testSchemeNameRecords = getService.testSchemeNameRecords(projectId);
		if(testSchemeNameRecords.size()>0){
			DataRow record = testSchemeNameRecords.get(0);
			String testSchemeId = param.get("PT_ID");
			//String projectId = param.get("PI_ID");
			if(StringUtil.isNullOrEmpty(testSchemeId)){
				testSchemeId = record.stringValue("PT_ID");
				//projectId = record.stringValue("PI_ID");
				this.setAttributes(record);
			}
			
			List<DataRow> formDataRecords = getService.findFormDataRecords(testSchemeId,projectId);//表格数据
			for (int i=0;i < formDataRecords.size();i++){
				DataRow row = formDataRecords.get(i);
				String defectNum = row.stringValue("PF_DEFECTNUM");
				if("0".equals(defectNum)){
					row.put("ELIMINATRATE", "0");
				}else{
					double eliminatRate = Double.parseDouble(row.stringValue("PF_COMPLETENUM"))/Double.parseDouble(defectNum);
					row.put("ELIMINATRATE", String.valueOf(eliminatRate));
				}
			}
			this.setRsList(formDataRecords);
		}
		this.setAttributes(param);
		this.processPageAttributes(param);
		this.testSchemeName(param);
		return new LocalRenderer(getPage());
	}
	
	public void testSchemeName(DataParam param){//测试方案列表
		ProjectDefectStatisticsManage getService = lookupService(ProjectDefectStatisticsManage.class);
		Project project = (Project)this.getSessionAttribute(Consts.ProjectSessionKey);
		String projectId = project.getProjectId();

		List<DataRow> testSchemeNameRecords = getService.testSchemeNameRecords(projectId);
		if(testSchemeNameRecords.size()>0){
			DataRow record = testSchemeNameRecords.get(0);
			String paramTestSchemeId = param.get("PT_ID");
			
			if(StringUtil.isNullOrEmpty(paramTestSchemeId)){
				record.put("css", "background-color:#579EFF;color:#fff;");
			}else{
				for(int i = 0; i<testSchemeNameRecords.size();i++){
					DataRow row = testSchemeNameRecords.get(i);
					String listTestSchemeId = row.getString("PT_ID");
					
					if(paramTestSchemeId.equals(listTestSchemeId)){
						row.put("css", "background-color:#579EFF;color:#fff;");
					}else{
						row.put("css", "");
					}
				}
			}
		}
		this.setAttribute("testSchemeNameRecords", testSchemeNameRecords);
	}
	
	protected void processPageAttributes(DataParam param){
		
	}	
	
	@PageAction
	public ViewRenderer projectDefectXml(DataParam param){//统计
		String result = "";
		String testSchemeId = param.get("testSchemeId");
		String projectId = param.get("projectId");
		String chartType = param.get("chartType");
		ProjectDefectStatisticsManage getService = lookupService(ProjectDefectStatisticsManage.class);
		if(chartType.equals("projectDefectChart")){
			List<DataRow> projectDefectRecords = getService.findProjectDefectRecords(testSchemeId,projectId);
			result = this.projectDefectChartXml(projectDefectRecords);
		}
		if(chartType.equals("eliminatRateChart")){
			List<DataRow> eliminatRateRecords = getService.findEliminatRateRecords(testSchemeId,projectId);
			result = this.eliminatRateXmlChartXml(eliminatRateRecords);
		}
		return new AjaxRenderer(result);
	}
	
	public String projectDefectChartXml(List<DataRow> projectDefectRecords){
		Document document = XmlUtil.createDocument();
		Element charElement = document.addElement("chart"); 

		charElement.addAttribute("caption","问题数最多的功能统计");
		charElement.addAttribute("yAxisName","问题数");
		charElement.addAttribute("useRoundEdges","1");
		charElement.addAttribute("showBorder","0");
		charElement.addAttribute("legendBorderAlpha","0");
		charElement.addAttribute("showBorder","1");
		charElement.addAttribute("borderColor","#b5b4b4");
		charElement.addAttribute("bgColor  ","#e8e8e8");
		for (int i=0;i < projectDefectRecords.size();i++){
			DataRow row = projectDefectRecords.get(i);
			Element element = charElement.addElement("set");
			element.addAttribute("label",row.stringValue("PF_NAME"));
			element.addAttribute("value",row.stringValue("PF_DEFECTNUM"));
		}
		if(projectDefectRecords.size()==0){
			Element setElement = charElement.addElement("set");
			setElement.addAttribute("value","0");
		}
		return document.asXML().toString();
	}
	
	public String eliminatRateXmlChartXml(List<DataRow> eliminatRateRecords){
		Document document = XmlUtil.createDocument();
		Element charElement = document.addElement("chart"); 

		charElement.addAttribute("caption","消缺率统计");
		charElement.addAttribute("yAxisName","消缺率");
		charElement.addAttribute("useRoundEdges","1");
		charElement.addAttribute("showBorder","0");
		charElement.addAttribute("legendBorderAlpha","0");
		charElement.addAttribute("showBorder","1");
		charElement.addAttribute("borderColor","#b5b4b4");
		charElement.addAttribute("bgColor  ","#e8e8e8");
		for (int i=0;i < eliminatRateRecords.size();i++){
			DataRow row = eliminatRateRecords.get(i);
			Element element = charElement.addElement("set");
			String defectNum = row.stringValue("PF_DEFECTNUM");
			if("0".equals(defectNum)){
				element.addAttribute("label",row.stringValue("PF_NAME"));
				element.addAttribute("value","0");
			}else{
				double eliminatRate = Double.parseDouble(row.stringValue("PF_COMPLETENUM"))/Double.parseDouble(row.stringValue("PF_DEFECTNUM"));
				element.addAttribute("label",row.stringValue("PF_NAME"));
				element.addAttribute("value",String.valueOf(eliminatRate));
			}
		}
		if(eliminatRateRecords.size()==0){
			Element setElement = charElement.addElement("set");
			setElement.addAttribute("value","0");
		}
		return document.asXML().toString();
	}
	
	@PageAction
	public ViewRenderer back(DataParam param){
		return new RedirectRenderer(getHandlerURL("MainWin"));
	}		
}
